<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="handleClose"
  >
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="deptRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width: 80%;" placeholder="2--10个字符" />
      </el-form-item>

      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" size="mini" style="width: 80%;" placeholder="2--10个字符" />
      </el-form-item>

      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="deptForm.managerId"
          size="mini"
          style="width: 80%;"
          placeholder="请选择负责人"
        >
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" :rows="4" size="mini" style="width: 80%;" placeholder="1-100个字符" />
      </el-form-item>

      <el-form-item>
        <el-row style="width: 80%" type="flex" justify="center">
          <el-button size="mini" type="primary" @click="confirmClick">确定</el-button>
          <el-button size="mini" @click="handleClose">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDeptmentsAPI, getManagersAPI, addDepartmentAPI, getDeptmentdetailAPI, updateDepartmentAPI } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      deptForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let deptList = await getDeptmentsAPI()
              if (this.deptForm.id) {
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              const isDouble = deptList.some(item => item.name === value)
              if (isDouble) {
                callback(new Error('部门名称已存在，不能重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let deptList = await getDeptmentsAPI()
              if (this.deptForm.id) {
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              const isDouble = deptList.some(item => item.code === value)
              if (isDouble) {
                callback(new Error('部门编码已存在，不能重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '部门编码负责人为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.deptForm.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    handleClose() {
      this.deptForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      this.managerList = await getManagersAPI()
    },
    confirmClick() {
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          if (this.deptForm.id) {
            await updateDepartmentAPI(this.deptForm)
            this.$message.success('修改部门成功')
          } else {
            await addDepartmentAPI({ ...this.deptForm, pid: this.parentId })

            this.$message.success('添加部门成功')
          }
          this.$emit('updateDepts')
          this.handleClose()
        }
      })
    },
    async getDepartmentDetail() {
      // console.log(this.parentId)
      this.deptForm = await getDeptmentdetailAPI(this.parentId)
    }
  }
}
</script>

<style scoped>

</style>
